package com.example.myapplication.Adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.Bean.communityBean;

import java.util.ArrayList;
import java.util.List;

public class commityApaer extends BaseAdapter {

    List<communityBean> list = new ArrayList<>();
    Activity activity;
    int flag;

    public commityApaer(List<communityBean> list, Activity activity, int flag) {
        this.list = list;
        this.activity = activity;
        this.flag = flag;
    }

    @Override
    // 返回数据列表的大小，即 AdapterView 中要显示的项数。
    public int getCount() {
        return list.size();
    }

    @Override
    // 返回指定位置的数据项。
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    // 返回指定位置的数据项的 ID。
    public long getItemId(int i) {
        return i;
    }

    @Override
    // 这个方法是最重要的，用于为每个列表项创建视图并绑定数据
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(activity).inflate(R.layout.lv_community, null);
        // 绑定视图控件
        ImageView imgTouXiang = view.findViewById(R.id.img_chihuo_item);
        TextView tvBiaoTi = view.findViewById(R.id.tv_chihuo_title);
        TextView tvName = view.findViewById(R.id.tv_chihuo_name);
        TextView tvTime = view.findViewById(R.id.tv_chihuo_time);
        TextView tvCount = view.findViewById(R.id.tv_chihuo_count);
        ImageView xin = view.findViewById(R.id.xin);

        // 绑定数据
        imgTouXiang.setImageResource(list.get(i).getImg());
        tvBiaoTi.setText(list.get(i).getBiaoti());
        tvName.setText(list.get(i).getName());
        tvTime.setText(list.get(i).getDate());
        tvCount.setText(list.get(i).getNum());

        if (flag == 1) {
            tvCount.setVisibility(View.VISIBLE);
            xin.setVisibility(View.VISIBLE);
            // 添加点击事件
            xin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    xin.setImageResource(R.drawable.heart2);
                    int count = Integer.parseInt(list.get(i).getNum());
                    count++;
                    list.get(i).setNum(String.valueOf(count));
                    tvCount.setText(String.valueOf(count));
                }
            });
        } else {
            tvCount.setVisibility(View.GONE);
            xin.setVisibility(View.GONE);
        }

        return view;
    }
}